<template>
	<view class="get_code">
		<view v-for="(item, index) in num" :key="index" class="code_view" @click="focus = true"
			:class="index<=code.length?'active':''">
			{{ code[index] && code[index] || '' }}
		</view>
		<input class="cinput" type="tel" v-model="code" :maxlength="num" :focus="focus" :cursor="code.length"
			@focus="focus=true" @blur="focus=false" @input="input" />
	</view>
	</view>
</template>

<script>
	export default {
		name: "getCode",
		props: ["num"],
		data() {
			return {
				focus: false,
				code: ""
			};
		},
		methods: {
			input(e) {
				let inputCode = e.detail.value
				if (inputCode.length == this.num) {
					this.$emit("inputCode", inputCode)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.get_code {
		display: flex;
		width: 100%;
		justify-content: center;
		align-items: center;
		position: relative;

		.code_view {
			width: 114rpx;
			height: 114rpx;
			border: 2rpx solid #aaaabb;
			margin-left: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 54rpx;
			font-weight: 600;

			&.code_view:first-child {
				margin-left: 0;
			}

			&.active {
				border: 4rpx solid #14151a;
			}
		}

		.cinput {
			position: absolute;
			top: 0;
			left: -100%;
			width: 100%;
			height: 100%;
		}
	}
</style>